package com.icow.wms.basic.controller;

import java.util.List;

import com.icow.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.icow.common.annotation.Log;
import com.icow.common.core.controller.BaseController;
import com.icow.common.core.domain.AjaxResult;
import com.icow.common.enums.BusinessType;
import com.icow.wms.basic.domain.WmsMatList;
import com.icow.wms.basic.service.IWmsMatListService;
import com.icow.common.utils.poi.ExcelUtil;
import com.icow.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料需求Controller
 *
 * @author lqq
 * @date 2022-01-14
 */
@RestController
@RequestMapping(value = {"/wms.basic/matList", "/api/matList"})
public class WmsMatListController extends BaseController {
    @Autowired
    private IWmsMatListService wmsMatListService;

    /**
     * 查询物料需求列表
     */
    @PreAuthorize("@ss.hasPermi('wms.basic:matList:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsMatList wmsMatList) {
        startPage();
        List<WmsMatList> list = wmsMatListService.selectWmsMatListList(wmsMatList);
        return getDataTable(list);
    }

    /**
     * 导出物料需求列表
     */
    @PreAuthorize("@ss.hasPermi('wms.basic:matList:export')")
    @Log(title = "物料需求", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WmsMatList wmsMatList) {
        List<WmsMatList> list = wmsMatListService.selectWmsMatListList(wmsMatList);
        ExcelUtil<WmsMatList> util = new ExcelUtil<WmsMatList>(WmsMatList.class);
        return util.exportExcel(list, "matList");
    }

    /**
     * 获取物料需求详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms.basic:matList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(wmsMatListService.selectWmsMatListById(id));
    }

    /**
     * 新增物料需求
     */
    @PreAuthorize("@ss.hasPermi('wms.basic:matList:add')")
    @Log(title = "物料需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsMatList wmsMatList) {
        return toAjax(wmsMatListService.insertWmsMatList(wmsMatList));
    }

    /**
     * 修改物料需求
     */
    @PreAuthorize("@ss.hasPermi('wms.basic:matList:edit')")
    @Log(title = "物料需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsMatList wmsMatList) {
        return toAjax(wmsMatListService.updateWmsMatList(wmsMatList));
    }

    /**
     * 删除物料需求
     */
    @PreAuthorize("@ss.hasPermi('wms.basic:matList:remove')")
    @Log(title = "物料需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (ids[0] == -1) {
            return toAjax(wmsMatListService.deleteWmsMatList());
        }
        return toAjax(wmsMatListService.deleteWmsMatListByIds(ids));
    }

    /**
     * http://localhost:8096/api/matList/upLoadData
     * [
     * {
     * "sn": "20220114021",
     * "status": "N"
     * },
     * {
     * "sn": "20220114022",
     * "status": "N"
     * }
     * ]
     *
     * @param dataList
     * @return
     * @throws Exception
     */
    @Log(title = "物料需求上传", businessType = BusinessType.IMPORT)
    @PostMapping("/upLoadData")
    public AjaxResult upLoadData(@RequestBody List<WmsMatList> dataList) throws Exception {
//        String operName = SecurityUtils.getUsername();
        String message = wmsMatListService.importWmsMatList(dataList, true, "system");
        return AjaxResult.success(message);
    }

    @PreAuthorize("@ss.hasPermi('wms.basic:matList:import')")
    @Log(title = "物料需求", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WmsMatList> util = new ExcelUtil<WmsMatList>(WmsMatList.class);
        List<WmsMatList> wmsMatListList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = wmsMatListService.importWmsMatList(wmsMatListList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<WmsMatList> util = new ExcelUtil<WmsMatList>(WmsMatList.class);
        return util.importTemplateExcel("物料需求");
    }
}